<?php

namespace App\Models\Concerns;

use App\Models\Product;
use App\Models\ProductRelation;

/**
 * Created by PhpStorm.
 * User: superxing-cto
 * Date: 17/2/6
 * Time: 10:59
 */
trait ProductTraits
{
    public function products()
    {
        return Product::select('products.*')
            ->join('product_relations', 'product_relations.product_id', '=', 'products.id')
            ->where('product_relations.type', $this->model_name())
            ->where('product_relations.type_id', $this->id)
            ->whereNull('product_relations.deleted_at')
            ->orderBy('product_relations.sort', 'desc')
            ->groupBy('products.id');
    }
}